package com.guigu.youxuan.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.pojo.BackUser;
import com.guigu.youxuan.pojo.OrderInfo;
import com.guigu.youxuan.pojo.SupplierInfo;
import com.guigu.youxuan.pojo.SysUsers;
import com.guigu.youxuan.pojo.Warehouse;
import com.guigu.youxuan.pojo.dto.LoginForm;
import com.guigu.youxuan.pojo.dto.MetaDto;
import com.guigu.youxuan.pojo.dto.SysUserDto;
import com.guigu.youxuan.service.ISysUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/sysUsers")
public class SysUsersController {
    @Autowired
    private ISysUsersService sysUsersService;

    /**
     * 前台登录
     * @param loginForm
     * @return
     */
    @RequestMapping("/Users/Login")
    @ResponseBody
    public SysUserDto login(@RequestBody LoginForm loginForm) {
        SysUsers sysUsers = sysUsersService.getSysUsers(loginForm);
        SysUserDto sysUserDto = new SysUserDto();
        MetaDto metaDto = new MetaDto();
        if (sysUsers != null) {
            // 登录成功
            sysUserDto.setSysUsers(sysUsers);

            UUID token = UUID.randomUUID();
            sysUserDto.setToken(token);

            metaDto.setMsg("登录成功");
            metaDto.setStatus(200);
            sysUserDto.setMetaDto(metaDto);

            return sysUserDto;
        }
        metaDto.setMsg("账号或密码错误！");
        metaDto.setStatus(500);
        sysUserDto.setMetaDto(metaDto);
        return sysUserDto;
    }

    /**
     * 查询用户名是否重复
     * @param username
     * @return
     */
    @RequestMapping("/selUserCount/{username}")
    @ResponseBody
    public String selUserCount(@PathVariable String username){
        String sysUsers = sysUsersService.selUserCount(username);
        return sysUsers;
    }

    /**
     * 注册
     * @param sysUsers
     * @return
     */
    @RequestMapping("/addSysUsers")
    @ResponseBody
    public String add(@RequestBody SysUsers sysUsers) {
        sysUsersService.addSysUsers(sysUsers);
        return "新增用户成功！";
    }

    /**
     * 删除
     *
     * @param sysUsers
     * @return
     */
    @RequestMapping("/delSysUser")
    @ResponseBody
    public String delSysUser(@RequestBody SysUsers sysUsers) {
        sysUsersService.delSysUser(sysUsers);
        return "删除成功！";
    }

    /**
     * 批量删除用户
     *
     * @param sysUserList
     * @return
     */
    @RequestMapping("/delsSysUser")
    @ResponseBody
    public String delsSysUser(@RequestBody List<SysUsers> sysUserList) {
        sysUsersService.delsSysUser(sysUserList);
        return "删除成功！";
    }

    /**
     * 用户冻结/正常
     *
     * @param sysUsers
     * @return
     */
    @RequestMapping("/switchState")
    public String switchState(@RequestBody SysUsers sysUsers) {
        sysUsersService.switchState(sysUsers);
        if (sysUsers.getState() == 0) {
            return "恢复成功";
        }
        return "冻结成功";
    }
    /**
     * 用户列表查询
     *
     * @param pageNo   当前页码
     * @param pageSize 每页显示的记录数
     * @param sysUsers 查询条件
     * @return 分页结果
     */
    @RequestMapping("/getSysUserList/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<SysUsers> page(@PathVariable int pageNo,
                                    @PathVariable int pageSize,
                                    @RequestBody SysUsers sysUsers) {
        // 调用 sysUsersService 的 getSysUsersList 方法查询用户列表
        PageUtils<SysUsers> pageInfo = this.sysUsersService.getSysUsersList(sysUsers, pageNo, pageSize);
        System.out.println("pageInfo = " + pageInfo);
        return pageInfo;
    }
    /**
     * 商户根据用户表id查询对应的商户信息
     * @param id
     * @return
     */
    @RequestMapping("/merchantInfoById/{id}")
    @ResponseBody
    public SysUsers merchantInfoById(@PathVariable int id){
        SysUsers goodsInfo=this.sysUsersService.getById(id);
        System.out.println("1111111 = " + goodsInfo);
        goodsInfo.setMerchantTag("S001-4");
        this.sysUsersService.updateById(goodsInfo);
        return goodsInfo;
    }

    /**
     * 商户根据用户表id查询对应的商户信息
     * @param id
     * @return
     */
    @RequestMapping("/supplierInfoById/{id}")
    @ResponseBody
    public SysUsers supplierInfoById(@PathVariable int id){
        SysUsers goodsInfo=this.sysUsersService.getById(id);
        System.out.println("1111111 = " + goodsInfo);
        goodsInfo.setSupplierTag("G001-4");
        this.sysUsersService.updateById(goodsInfo);
        return goodsInfo;
    }


    /**
     * 通过id查询到要修改的用户
     * @param sysUsersId
     * @return
     */
    @RequestMapping("/sysUsersById/{sysUsersId}")
    @ResponseBody
    public SysUsers sysUsersById(@PathVariable int sysUsersId){
        SysUsers sysUsers=this.sysUsersService.getById(sysUsersId);
        return sysUsers;
    }


    /**
     * 对用户个人信息修改
     * @param sysUsers
     * @return
     */
    @RequestMapping("/sysUserUpd")
    @ResponseBody
    public String sysUserUpd(@RequestBody SysUsers sysUsers){
        boolean flag=this.sysUsersService.updateById(sysUsers);
        if (flag){
            return "ok";
        }
        return "error";
    }
}
